package com.zhilianbao.leyaogo.ui.fragment.base;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.utils.KeyboardUtils;
import com.zhilianbao.okhttputils.OkHttpUtils;

import rx.Subscription;

/**
 * ClassName: BaseSearchFragment <br>
 * Description: 搜索基类Fragment<br>
 * Author: Cyarie <br>
 * Created: 2016/10/26 09:31 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public abstract class BaseSearchFragment<T extends Object> extends RefreshAndLoadFragment<T> {

    private Subscription mSubscription;

    protected String keyword;

    protected LoadingViewCallback mLoadingViewCallback;


    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_ptr_load_search;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    protected void initToolbarHere() {
        initSearchToolbar("");
        mLlSearchBar.setBackgroundColor(getResources().getColor(R.color.color_f0f0f0));
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mEditText.setFocusableInTouchMode(true);
        mEditText.setFocusable(true);
        //        mSubscription = RxTextView.textChangeEvents(mEditText)
        //                .debounce(800, TimeUnit.MILLISECONDS)
        //                .observeOn(AndroidSchedulers.mainThread())
        //                .subscribe(new Observer<TextViewTextChangeEvent>() {
        //                    @Override
        //                    public void onCompleted() {
        //                    }
        //
        //                    @Override
        //                    public void onError(Throwable e) {
        //                    }
        //
        //                    @Override
        //                    public void onNext(TextViewTextChangeEvent textViewTextChangeEvent) {
        //                        OkHttpUtils.getInstance().cancelTag(mActivity);
        //                        if (!CheckUtils.isEmpty(textViewTextChangeEvent.text())) {
        //                            keyword = textViewTextChangeEvent.text().toString();
        //                            loadDataList(1, false);
        //
        //                        }
        //                    }
        //                });

        mEditText.addTextChangedListener(mTextWatcher);
        KeyboardUtils.showKeyboard(mActivity, mEditText);

    }


    @Override
    public boolean canRefresh() {
        return false;
    }

    @Override
    public void btnClearClick() {
        super.btnClearClick();
        reset();
        KeyboardUtils.showKeyboard(mActivity);
        mEditText.requestFocus();
    }

    private TextWatcher mTextWatcher = new TextWatcher() {

        private String tmp = "";

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            tmp = s.toString();
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String str = s.toString();
            if (str.equals(tmp)) {
                return;
            }
            if (mEditText.getText().length() == 0) {
                mIvClear.setVisibility(View.GONE);
                reset();
            } else {
                mIvClear.setVisibility(View.VISIBLE);
            }

        }
    };

    /**
     * 重置页面
     */
    private void reset() {
        OkHttpUtils.getInstance().cancelTag(mActivity);
        if (getAdapter() != null) {
            mList.clear();
            mEditText.setText("");
            getAdapter().notifyDataSetChanged();
            if (mLoadingViewCallback != null) {
                mLoadingViewCallback.hideAllView();
            }
        }
    }


    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    public void onDestroyView() {
        if (mTextWatcher != null && mEditText != null) {
            mEditText.removeTextChangedListener(mTextWatcher);
            mTextWatcher = null;
        }
        super.onDestroyView();
        if (mSubscription != null) {
            mSubscription.unsubscribe();
        }

    }

}
